<?php

class DistributorGroup extends DataExtension {
    private static $db = array('IsDistributorGroup' => 'Boolean');

    function requireDefaultRecords() {
        if(!$distributorGroup = Group::get()->find('Code', 'free')) {
            Group::create()
            ->setField('Code', 'free')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', _t('Distributor.DefaultGroupTitleDistributor', 'Free Member'))
            ->setField('Sort', 1)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
		
		if(!$distributorGroup = Group::get()->find('Code', 'level1')) {
            Group::create()
            ->setField('Code', 'level1')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', 'Level 1 Member')
            ->setField('Sort', 2)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
		
		if(!$distributorGroup = Group::get()->find('Code', 'level2')) {
            Group::create()
            ->setField('Code', 'level2')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', 'Level 2 Member')
            ->setField('Sort', 3)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
		
		if(!$distributorGroup = Group::get()->find('Code', 'level3')) {
            Group::create()
            ->setField('Code', 'level3')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', 'Level 3 Member')
            ->setField('Sort', 4)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
		
		if(!$distributorGroup = Group::get()->find('Code', 'level4')) {
            Group::create()
            ->setField('Code', 'level4')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', 'Level 4 Member')
            ->setField('Sort', 5)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
		
		if(!$distributorGroup = Group::get()->find('Code', 'level5')) {
            Group::create()
            ->setField('Code', 'level5')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', 'Level 5 Member')
            ->setField('Sort', 6)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
		
		if(!$distributorGroup = Group::get()->find('Code', 'level6')) {
            Group::create()
            ->setField('Code', 'level6')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', 'Level 6 Member')
            ->setField('Sort', 7)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
		
		if(!$distributorGroup = Group::get()->find('Code', 'level7')) {
            Group::create()
            ->setField('Code', 'level7')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', 'Level 7 Member')
            ->setField('Sort', 8)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
		
		if(!$distributorGroup = Group::get()->find('Code', 'level8')) {
            Group::create()
            ->setField('Code', 'level8')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', 'Level 8 Member')
            ->setField('Sort', 9)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
		
		if(!$distributorGroup = Group::get()->find('Code', 'level9')) {
            Group::create()
            ->setField('Code', 'level9')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', 'Level 9 Member')
            ->setField('Sort', 10)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
		
		if(!$distributorGroup = Group::get()->find('Code', 'level10')) {
            Group::create()
            ->setField('Code', 'level10')
            ->setField('IsDistributorGroup', 1)
            ->setField('Title', 'Level 10 Member')
            ->setField('Sort', 11)
            ->write();
            DB::alteration_message('Distributor group created', 'created');
        }
    }

    function updateCMSFields(FieldList $fields) {
    	$fields->insertAfter(TextField::create('Code', $this->owner->fieldLabel('Code')), 'Title');
        $fields->insertAfter(CheckboxField::create('IsDistributorGroup', _t('DistributorGroup.IS_DISTRIBUTOR_GROUP', "This is the distributor group.")), 'ParentID');
    }
	
	function getTitleByLang(){
		if($this->owner->IsDistributorGroup){
			return _t(sprintf('DistributorGroup.%s', strtoupper($this->owner->Code)), $this->owner->Title);
		}
		
		return $this->owner->Title;
	}
}
